package com.runa.rccprotocol.dto.apportionCollector;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.monitor.comm.entity.CommMeter;
import com.runa.persistence.RepositoryServicePersis;
import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.dto.ModbusReadRequestData;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.enums.ModbusFunctionCode;
import com.runa.protocol.enums.ModbusType;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import com.runa.rccprotocol.em.ApportionCollectorModbusTypeEnum;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ApportionCollectorRequestData extends ConcentratorRequestData {

    public ApportionCollectorRequestData(String address) {
        super(address);
    }

    private byte[] requestData;

    public static void main(String[] args) {

//        String netSerial = "220927101";
//        String nodeSerial = "00000070032019";
        String netSerial = "202103091";
        String nodeSerial = "21000010240791";

        ApportionCollectorRequestData requestData = new ApportionCollectorRequestData(netSerial);
        requestData.valveCtrl(nodeSerial, 0x55);
        System.out.println("强开: " + ByteUtil.byteToHexString(requestData.getData()));
        requestData.valveCtrl(nodeSerial, 0x99);
        System.out.println("强关: " + ByteUtil.byteToHexString(requestData.getData()));
        requestData.readValve(nodeSerial);
        System.out.println("读阀: " + ByteUtil.byteToHexString(requestData.getData()));
        requestData.valveCtrl(nodeSerial, 0xFF);
        System.out.println("自由模式: " + ByteUtil.byteToHexString(requestData.getData()));
        requestData.valveCtrl(nodeSerial, 0x11);
        System.out.println("锁定开度: " + ByteUtil.byteToHexString(requestData.getData()));
        requestData.valveCtrl(nodeSerial, 0x88);
        System.out.println("调试模式: " + ByteUtil.byteToHexString(requestData.getData()));

        requestData.valveOnOffControl(nodeSerial, 4);
        System.out.println("设置阀门开度: " + ByteUtil.byteToHexString(requestData.getData()));

        requestData.readOpenOnDebug(nodeSerial);
        System.out.println("读取调试模式角度设置: " + ByteUtil.byteToHexString(requestData.getData()));

        requestData.readParamOnDebug(nodeSerial);
        System.out.println("读调试模式参数: " + ByteUtil.byteToHexString(requestData.getData()));

        requestData.angleSetOnDebug(nodeSerial, (byte)0x55);
        System.out.println("设置调试模式角度调节: " + ByteUtil.byteToHexString(requestData.getData()));

        requestData.setTempOnDebug(nodeSerial, 25, 1, 5);
        System.out.println("设置调试模式设定温度: " + ByteUtil.byteToHexString(requestData.getData()));



    }

    public void setTempOnDebug(String serial, int temp, int tolerance, int period) {
        String t = String.valueOf(temp);
        while (t.length() < 4) {
            t = "0" + t;
        }

        String to = String.valueOf(tolerance);
        while (to.length() < 4) {
            to = "0" + to;
        }

        String p = String.valueOf(period);
        while (p.length() < 4) {
            p = "0" + p;
        }

        byte[] data = new byte[100];
        int i = 0;
        data[i++] = 0x68;
        data[i++] = 0x46;
        ByteUtil.tenstringToBcd(data, serial, 2, false);
        i += 7;
        data[i++] = 0x04;
        data[i++] = 0x09;
        data[i++] = (byte)0xD0;
        data[i++] = 0x24;
        data[i++] = 0x01;
        byte[] tempBytes = ByteUtil.hexStringToByte(t);
        for (byte b : tempBytes) {
            data[i++] = b;
        }
        byte[] toleranceBytes = ByteUtil.hexStringToByte(to);
        for (byte b : toleranceBytes) {
            data[i++] = b;
        }
//        data[i++] = 0x00;
//        data[i++] = 0x01;
        byte[] periodBytes = ByteUtil.hexStringToByte(p);
        for (byte b : periodBytes) {
            data[i++] = b;
        }
        data[i++] = ByteUtil.sum(data, 0, i);
        data[i++] = 0x16;
        byte[] bs = new byte[i];
        System.arraycopy(data, 0, bs, 0, i);
        requestData = dataForward(bs);
        setHaveResult(true);


    }

    private void readParamOnDebug(String serial) {
        String hexS = "68 46 00 00 00 00 00 00 00 01 03 D0 26 01 00 16";
        byte[] data = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(data, serial, 2, false);
        data[data.length - 2] = ByteUtil.sum(data, 0, data.length - 2);
        requestData = dataForward(data);
        setHaveResult(true);
    }

    private void angleSetOnDebug(String serial, byte mode) {
        String hexS = "68 46 A0 A1 A2 A3 A4 A5 A6 04 04 D0 22 01 55 00 16";
        byte[] data = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(data, serial, 2, false);
        data[data.length - 3] = mode;
        data[data.length - 2] = ByteUtil.sum(data, 0, data.length - 2);
        requestData = dataForward(data);
        setHaveResult(true);
    }

    private void readOpenOnDebug(String serial) {
        String hexS = "68 46 00 00 00 00 00 00 00 01 03 D0 23 01 00 16";
        byte[] data = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(data, serial, 2, false);
        data[data.length - 2] = ByteUtil.sum(data, 0, data.length - 2);
        requestData = dataForward(data);
        setHaveResult(true);
    }

    private byte[] dataForward(byte[] data) {
        byte[] bytes = new byte[100];
        int i = 0;
        bytes[i++] = 0x68;
        int l = 12 + data.length;
        bytes[i++] = (byte)(l & 0xff);
        bytes[i++] = (byte)(l >> 8 & 0xff);
        bytes[i++] = (byte)(l & 0xff);
        bytes[i++] = (byte)(l >> 8 & 0xff);
        bytes[i++] = 0x68;
        bytes[i++] = 0x41;
        bytes[i++] = 0x02;
        bytes[i++] = 0x37;
        bytes[i++] = 0x01;
        bytes[i++] = 0x00;
        bytes[i++] = 0x00;
        bytes[i++] = 0x55;
        bytes[i++] = (byte)0xE0;
        bytes[i++] = 0x00;
        bytes[i++] = 0x00;
        bytes[i++] = 0x01;
        bytes[i++] = 0x00;

        for (byte d : data) {
            bytes[i++] = d;
        }
        bytes[i++] = ByteUtil.sum(bytes, 6, i - 6);
        bytes[i++] = 0x16;
        byte[] r = new byte[i];
        System.arraycopy(bytes, 0, r, 0, r.length);
        return r;
    }


    public void readModbus(Integer slaveId, String model) {
        //14位编号
        String hexS = "68 14 00 14 00 68 " +
                "41 02 37 01 00 00 " +
                "55 E0 00 00 01 00 " +
                "01 03 00 00 00 00 00 00 " +
                "65 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        int index = 18;
        byte[] modbusBytes = ApportionCollectorModbusTypeEnum.getModbusBytes(slaveId, model);
        for (int i = 0; i < modbusBytes.length; i++) {
            requestData[index++] = modbusBytes[i];
        }
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 20);
        setHaveResult(true);
    }


    public void readHeatMeter(String meter) {
        if (meter.startsWith("F")) {
            //14位编号
            String hexS = "68 1C 00 1C 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 20 88 10 24 10 00 00 00 01 03 90 1F 01 4F 16 65 16";
            requestData = ByteUtil.hexStringToByte(hexS);
            ByteUtil.stringToBcd(requestData, meter, 20, true);
        } else {
            //14位编号
            String hexS = "68 1C 00 1C 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 20 88 10 24 10 00 00 00 01 03 1F 90 01 4F 16 65 16";
            requestData = ByteUtil.hexStringToByte(hexS);
            ByteUtil.stringToBcd(requestData, meter, 20, false);
        }

        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 14);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 28);
        setHaveResult(true);
    }

    public void readHeatMeterPG(int address) {
        ModbusReadRequestData readRequestData = new ModbusReadRequestData(address,
                ModbusFunctionCode.READ_INPUT_REGISTERS, 99, 36, ModbusType.MODBUS_RTU);
        byte[] modbus = readRequestData.getData();
        int len  = 12 + modbus.length;
        requestData = new byte[8+len];
        requestData[0] = 0x68;
        ByteUtil.shortToByte((short) len, requestData, 1, ByteOrder.BA);
        ByteUtil.shortToByte((short) len, requestData, 3, ByteOrder.BA);
        requestData[5] = 0x68;
        requestData[6] = 0x41;
        requestData[7] = 0x02;
        requestData[8] = 0x37;
        requestData[9] = 0x01;
        requestData[10] = 0x00;
        requestData[11] = 0x0;
        requestData[12] = 0x55;
        requestData[13] = (byte) 0xe0;
        requestData[14] = 0;
        requestData[15] = 0;
        requestData[16] = 1;
        requestData[17] = 0;
        System.arraycopy(modbus, 0, requestData, 18, modbus.length);
        requestData[requestData.length-2] = ByteUtil.sum(requestData, 6, requestData.length-8);
        requestData[requestData.length-1] = 0x16;
        setHaveResult(true);
    }


    public void readMeter(String meter) {
        String hexS = "68 11 00 11 00 68 " +
                "41 02 37 01 00 00 " +
                "0C E0 00 00 40 03 " +
                "00 00 00 00 00 00 00 " +
                "CC 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        int len = requestData.length - 6 - 2;
        requestData[1] = (byte)(len & 0xff);
        requestData[2] = (byte)(len >> 8 & 0xff);
        requestData[3] = (byte)(len & 0xff);
        requestData[4] = (byte)(len >> 8 & 0xff);
        ByteUtil.stringToBcd(requestData, meter, 18, false);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, len);
        setHaveResult(true);
    }


    public void readValveData(String valveSerial) {
        //14位编号
        String hexS = "68 11 00 11 00 68 " +
                "41 02 37 01 00 00 " +
                "0C E0 00 00 20 03 " +
                "00 00 00 00 00 00 00 " +
                "2D 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        ByteUtil.stringToBcd(requestData, valveSerial, 18, false);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 17);
        setHaveResult(true);
    }


    public void readValve(String valveSerial) {
        //14位编号
        String hexS = "68 1C 00 1C 00 68 " +
                "41 02 37 01 00 00 " +
                "55 E0 00 00 01 00 " +
                "68 46 " +
                "88 10 24 10 00 00 00 " +
                "01 03 90 1F 01 " +
                "4F 16 " +
                "65 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(requestData, valveSerial, 20, false);
        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 14);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 28);
        setHaveResult(true);
    }

    public void enterFreeControlMode(String serial) {
        String hexS = "68 1D 00 1D 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 46 88 10 24 10 00 00 00 04 04 A0 17 01 FF 5A 16 00 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(requestData, serial, 20, false);
        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 15);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 29);
        setHaveResult(true);
    }

    public void openTheValveOnce(String serial) {
        String hexS = "68 1D 00 1D 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 46 88 10 24 10 00 00 00 04 04 A0 17 01 66 C1 16 49 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(requestData, serial, 20, false);
        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 15);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 29);
        setHaveResult(true);
    }

    public void valveCtrl(String serial, int ctrl) {
        String hexS = "68 1D 00 1D 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 46 88 10 24 10 00 00 00 04 04 A0 17 01 11 6C 16 00 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(requestData, serial, 20, false);
        requestData[requestData.length - 5] = (byte) ctrl;
        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 15);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 29);
        setHaveResult(true);
    }

    public void valveOnOffControl(String serial, Integer open) {
        String hexS = "68 1D 00 1D 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 46 88 10 24 10 00 00 00 04 04 D0 20 01 60 F4 16 AF 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        requestData[requestData.length - 5] = open.byteValue();
        ByteUtil.tenstringToBcd(requestData, serial, 20, false);
        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 15);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 29);
        setHaveResult(true);
    }


    /**
     * 温控-调试模式
     */
    public void enterDebugMode(String serial) {
        String hexS = "68 1D 00 1D 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 46 88 10 24 10 00 00 00 04 04 A0 17 01 88 E3 16 8D 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(requestData, serial, 20, false);
        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 15);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 29);
        setHaveResult(true);
    }

    public void setDebugModeAngleAdjustment(String serial) {
        String hexS = "68 1D 00 1D 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 46 88 10 24 10 00 00 00 04 04 D0 22 01 55 EB 16 9D 16";
        requestData = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(requestData, serial, 20, false);
        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 15);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 29);
        setHaveResult(true);
    }

    public void setDebuggingModeSetTheTemperature(String serial, Float tem) {
        String hexS = "68 22 00 22 00 68 41 02 37 01 00 00 55 E0 00 00 01 00 68 46 88 10 24 10 00 00 00 04 09 D0 24 01 00 22 00 01 10 00 D0 16 00 16";
        String str = String.valueOf(tem * 100).substring(2, 4) + String.valueOf(tem * 100).substring(0, 2);
        hexS = hexS.replace("24 01 00 22", "24 01 " + str);
        requestData = ByteUtil.hexStringToByte(hexS);
        ByteUtil.tenstringToBcd(requestData, serial, 20, false);
        requestData[requestData.length - 4] = ByteUtil.sum(requestData, 18, 20);
        requestData[requestData.length - 2] = ByteUtil.sum(requestData, 6, 34);
        setHaveResult(true);
    }



    @Override
    public boolean check(IResponseData data) {
        log.debug("正定响应校验");
        return true;
    }

    @Override
    public byte[] getData() {
        return this.requestData;
    }


}
